package com.hwhp.service.gh.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.hwhp.dao.BaseDao;
import com.hwhp.entity.SGhRecord;
import com.hwhp.entity.SLstOfVal;
import com.hwhp.entity.SPatient;
import com.hwhp.service.gh.RecordService;
import com.hwhp.util.DateUtils;
import com.hwhp.web.gh.vo.RecordVo;

public class RecordServiceImpl implements RecordService {
	 private BaseDao baseDao = null;

	 
	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public List queryList() {
		// TODO Auto-generated method stub
		String hql = "from SGhRecord";
		List recordList = this.baseDao.query(hql);
		List recordListTemp = new ArrayList();
		for (int i=0;i<recordList.size();i++){
			SGhRecord sGhRecord = (SGhRecord)recordList.get(i);
			String departId = sGhRecord.getDepartment();
			String typeId = sGhRecord.getType();
			if(!departId.equalsIgnoreCase("0")){
				SLstOfVal departVo = (SLstOfVal)this.baseDao.get(SLstOfVal.class, departId);
				sGhRecord.setDepartment(departVo.getName());
			}
			else{
				sGhRecord.setDepartment("");
			}
				
			if(!typeId.equalsIgnoreCase("0")){
			SLstOfVal typeVo = (SLstOfVal)this.baseDao.get(SLstOfVal.class, typeId);
			sGhRecord.setType(typeVo.getName());
			}
			else{
				sGhRecord.setType("");
			}
			recordListTemp.add(sGhRecord);
		}
		return recordListTemp;
	}

	public void add(RecordVo recordVo) {
		// TODO Auto-generated method stub
		SGhRecord sGhRecord = new SGhRecord();
		SPatient sPatient = new SPatient();
		sGhRecord.setNum(recordVo.getNum());
		sGhRecord.setName(recordVo.getName());
		sGhRecord.setDepartment(recordVo.getDepartment());
		sGhRecord.setDoctor(recordVo.getDoctor());
		sGhRecord.setType(recordVo.getType());
		sGhRecord.setIsFirst(recordVo.getIsFirst());
		String pattern = "yyyy-MM-dd HH:mm:ss";
		sGhRecord.setGhDate(DateUtils.formatNowtime(pattern));
		
		sPatient.setNum(recordVo.getNum());
		sPatient.setName(recordVo.getName());
		//sPatient.setBirthday(recordVo.getBirthday());
		sPatient.setIdType(recordVo.getIdType());
		sPatient.setProfessional(recordVo.getProfessional());
		sPatient.setSex(recordVo.getSex());
		sPatient.setTelephone(recordVo.getTelephone());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			sPatient.setBirthday(format.parse(recordVo.getBirthday()));
		} catch (ParseException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		try {
			this.baseDao.save(sGhRecord);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			this.baseDao.save(sPatient);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public RecordVo preUpdate(String id) {
		// TODO Auto-generated method stub
		String hql = "from SGhRecord as r where r.id = '"+id+"'";
		SGhRecord sGhRecord = (SGhRecord)this.baseDao.query(hql).get(0);
		RecordVo recordVo = new RecordVo();
		recordVo.setId(sGhRecord.getId());
		recordVo.setNum(sGhRecord.getNum());
		recordVo.setName(sGhRecord.getName());
		recordVo.setDepartment(sGhRecord.getDepartment());
		recordVo.setDoctor(sGhRecord.getDoctor());
		recordVo.setType(sGhRecord.getType());
		recordVo.setIsFirst(sGhRecord.getIsFirst());
		String hqlPatient = "from SPatient as r where r.num = '"+sGhRecord.getNum()+"'";
		SPatient sPatient = (SPatient)this.baseDao.query(hqlPatient).get(0);
		recordVo.setIdType(sPatient.getIdType());
		recordVo.setProfessional(sPatient.getProfessional());
		recordVo.setSex(sPatient.getSex());
		recordVo.setIdNum(sPatient.getIdNum());
		recordVo.setTelephone(sPatient.getTelephone());
		recordVo.setPatientId(sPatient.getId());
		return recordVo;
	}

	public void update(RecordVo recordVo) {
		// TODO Auto-generated method stub
		SGhRecord sGhRecord = new SGhRecord();
		SPatient sPatient = new SPatient();
		sGhRecord.setNum(recordVo.getNum());
		sGhRecord.setName(recordVo.getName());
		sGhRecord.setDepartment(recordVo.getDepartment());
		sGhRecord.setDoctor(recordVo.getDoctor());
		sGhRecord.setType(recordVo.getType());
		sGhRecord.setId(recordVo.getId());
		recordVo.setIsFirst(sGhRecord.getIsFirst());
		
		sPatient.setId(recordVo.getPatientId());
		sPatient.setNum(recordVo.getNum());
		sPatient.setName(recordVo.getName());
		//sPatient.setBirthday(recordVo.getBirthday());
		sPatient.setIdType(recordVo.getIdType());
		sPatient.setProfessional(recordVo.getProfessional());
		sPatient.setSex(recordVo.getSex());
		sPatient.setIdNum(recordVo.getIdNum());
		sPatient.setTelephone(recordVo.getTelephone());
		this.baseDao.update(sGhRecord);
		this.baseDao.update(sPatient);
	}

	public List queryListByNum(String num) {
		// TODO Auto-generated method stub
		String hql = "from SGhRecord as r where r.num = '"+num+"'";
		return this.baseDao.query(hql);
	}

	public void delete(String id) {
		// TODO Auto-generated method stub
		try {
			this.baseDao.delete(this.baseDao.get(SGhRecord.class, id));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public RecordVo SearchPatient(String num) {
		// TODO Auto-generated method stub
		String hql = "from SPatient as r where r.num = '"+num+"'";
		SPatient sPatient = (SPatient)this.baseDao.query(hql).get(0);
		RecordVo recordVo = new RecordVo();
		recordVo.setIdType(sPatient.getIdType());
		recordVo.setProfessional(sPatient.getProfessional());
		recordVo.setSex(sPatient.getSex());
		recordVo.setIdNum(sPatient.getIdNum());
		recordVo.setTelephone(sPatient.getTelephone());
		recordVo.setPatientId(sPatient.getId());
		recordVo.setNum(sPatient.getNum());
		recordVo.setName(sPatient.getName());
		return recordVo;
	} 
	 
}
